﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;


public partial class View_ManageTimeTable : System.Web.UI.Page
{
    private ManageTimeTable mnTimeTable;
    private string SESSION_SEARCH = "SEARCH";
    protected void Page_Load(object sender, EventArgs e)
    {
        mnTimeTable = new ManageTimeTable();
        if (!IsPostBack)
        {
            Session.Remove(SESSION_SEARCH);
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string stringSearch = txtKeyWord.Text.Trim();
        if (txtKeyWord.Text.Trim() == String.Empty)
        {
            stringSearch = hdfSearchString.Value.Trim();
        }

        if (Session[SESSION_SEARCH] != null)
        {
            if (hdfSearchString.Value.Trim() != Session[SESSION_SEARCH].ToString())
            {
                stringSearch = hdfSearchString.Value.Trim();
            }
            if (Session[SESSION_SEARCH].ToString() != txtKeyWord.Text.Trim())
            {
                stringSearch = txtKeyWord.Text.Trim();
            }
        }

        hdfSearchString.Value = stringSearch;
        rdGTimeTable.DataSource = mnTimeTable.searchTimeTable(stringSearch);
        rdGTimeTable.Rebind();
        Session[SESSION_SEARCH] = stringSearch;
        txtKeyWord.Text = stringSearch;
    }

    protected void rdGTimeTable_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;

        int pageIndex = rdGTimeTable.CurrentPageIndex;
        String IDTimeTable = editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IDTimeTable"].ToString();
        TimeTable timeTable = new TimeTable();
        timeTable.IDTimeTable = Convert.ToInt32(IDTimeTable);
        int _result = mnTimeTable.deleteTimeTable(timeTable);
        if (_result == 1)
            Message.Show(rapTimeTable, "Xóa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapTimeTable, "Xóa dữ liệu không thành công1", Message.Type.Error);
        else
            Message.Show(rapTimeTable, "Xóa không thành công : Lỗi hệ thống", Message.Type.Error);
        rdGTimeTable.CurrentPageIndex = pageIndex;
    }

    protected void rdGTimeTable_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;

        RadComboBox ddlNameBusTrip = editedItem.FindControl("ddlNameBusTrip") as RadComboBox;
        RadComboBox ddlIDBus = editedItem.FindControl("ddlIDBus") as RadComboBox;
        RadComboBox ddlIDDriver = editedItem.FindControl("ddlIDDriver") as RadComboBox;
        RadComboBox ddlDirection = editedItem.FindControl("ddlDirection") as RadComboBox;

        RadDatePicker rdTimeStart = editedItem.FindControl("rdTimeStart") as RadDatePicker;
        RadDatePicker rdTimeStop = editedItem.FindControl("rdTimeStop") as RadDatePicker;
        TextBox txtMONEY = editedItem.FindControl("txtMONEY") as TextBox;

        TimeTable timeTable = new TimeTable();
        timeTable.IDBusStrip = Convert.ToInt32(ddlNameBusTrip.SelectedValue);
        timeTable.IDDriver = Convert.ToInt32(ddlIDDriver.SelectedValue);
        timeTable.IDBus = Convert.ToInt32(ddlIDBus.SelectedValue);
        if (ddlDirection.SelectedIndex == 0)
            timeTable.Direction = "Chiều đi";
        else
            timeTable.Direction = "Chiều về";

        timeTable.Money = (float)Convert.ToDouble(txtMONEY.Text);

        DateTime timeStart;
        DateTime.TryParse(rdTimeStart.SelectedDate.ToString(), out timeStart);
        timeTable.TimeStart = timeStart;

        DateTime TimeStop;
        DateTime.TryParse(rdTimeStop.SelectedDate.ToString(), out TimeStop);
        timeTable.TimeStopExpected = TimeStop;

        int _result = mnTimeTable.insertNewTimeTable(timeTable);
        if (_result >= 1)
            Message.Show(rapTimeTable, "Thêm dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapTimeTable, "Thêm dữ liệu không thành công: đã tồn tại xe", Message.Type.Error);
        else
            Message.Show(rapTimeTable, "Thêm không thành công : Lỗi hệ thống", Message.Type.Error);
        rdGTimeTable.CurrentPageIndex = 0;
    }

    /// <summary>
    /// ham bind du lieu
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGTimeTable_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {

        if ((Session[SESSION_SEARCH] != null) && (Session[SESSION_SEARCH].ToString() != string.Empty))
        {
            rdGTimeTable.DataSource = mnTimeTable.searchTimeTable(Session[SESSION_SEARCH].ToString());
        }
        else
        {
            rdGTimeTable.DataSource = mnTimeTable.getAllTimeTable();
        }
    }

    protected void rdGTimeTable_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;

        int pageIndex = rdGTimeTable.CurrentPageIndex;
        String IDTimeTable = editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IDTimeTable"].ToString();
        

        RadComboBox ddlNameBusTrip = editedItem.FindControl("ddlNameBusTrip") as RadComboBox;
        RadComboBox ddlIDBus = editedItem.FindControl("ddlIDBus") as RadComboBox;
        RadComboBox ddlIDDriver = editedItem.FindControl("ddlIDDriver") as RadComboBox;
        RadComboBox ddlDirection = editedItem.FindControl("ddlDirection") as RadComboBox;

        RadDatePicker rdTimeStart = editedItem.FindControl("rdTimeStart") as RadDatePicker;
        RadDatePicker rdTimeStop = editedItem.FindControl("rdTimeStop") as RadDatePicker;
        TextBox txtMONEY = editedItem.FindControl("txtMONEY") as TextBox;

        TimeTable timeTable = new TimeTable();
        timeTable.IDTimeTable = Convert.ToInt32(IDTimeTable);
        timeTable.IDBusStrip = Convert.ToInt32(ddlNameBusTrip.SelectedValue);
        timeTable.IDDriver = Convert.ToInt32(ddlIDDriver.SelectedValue);
        timeTable.IDBus = Convert.ToInt32(ddlIDBus.SelectedValue);
        if (ddlDirection.SelectedIndex == 0)
            timeTable.Direction = "Chiều đi";
        else
            timeTable.Direction = "Chiều về";

        timeTable.Money = (float)Convert.ToDouble(txtMONEY.Text);

        DateTime timeStart;
        DateTime.TryParse(rdTimeStart.SelectedDate.ToString(), out timeStart);
        timeTable.TimeStart = timeStart;

        DateTime TimeStop;
        DateTime.TryParse(rdTimeStop.SelectedDate.ToString(), out TimeStop);
        timeTable.TimeStopExpected = TimeStop;

        int _result = mnTimeTable.updateNewTimeTable(timeTable);
        if (_result == 1)
            Message.Show(rapTimeTable, "Chỉnh sửa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapTimeTable, "Chỉnh sửa dữ liệu không thành công", Message.Type.Error);
        else
            Message.Show(rapTimeTable, "Chỉnh sửa không thành công : Lỗi hệ thống", Message.Type.Error);
        rdGTimeTable.CurrentPageIndex = pageIndex; 
    }

    /// <summary>
    /// ham do du lieu vao tung hang
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGTimeTable_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            ImageButton Edit = ((ImageButton)item["Edit"].Controls[0]);
            Edit.CssClass = "btnEditDelete";
            Edit.Width = 20;
            Edit.Height = 20;
            Edit.Attributes.Add("style", "background-color:transparent");
            ImageButton Delete = ((ImageButton)item["Delete"].Controls[0]);
            Delete.CssClass = "btnEditDelete";
            Delete.Width = 20;
            Delete.Height = 20;
            Delete.Attributes.Add("style", "background-color:transparent;");
        }
        if (e.Item.IsInEditMode)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            Button btnAdd = new Button();
            btnAdd = item.FindControl("PerformInsertButton") as Button;
            if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
            {
                btnAdd.Text = "Thêm mới";
                btnAdd.CssClass = "myButton";
                RadComboBox ddlNameBusTrip = item.FindControl("ddlNameBusTrip") as RadComboBox;
                if (ddlNameBusTrip != null)
                {
                    ddlNameBusTrip.Items.Clear();
                    ddlNameBusTrip.DataSource = mnTimeTable.getAllBusTripActive();
                    ddlNameBusTrip.DataTextField = BusStripColumn.NameBusTrip;
                    ddlNameBusTrip.DataValueField = BusStripColumn.IDBusTrip;
                    ddlNameBusTrip.DataBind();
                    ddlNameBusTrip.SelectedValue = (item.FindControl("lblBusTripID") as Label).Text;
                }
                RadComboBox ddlIDDriver = item.FindControl("ddlIDDriver") as RadComboBox;
                if (ddlIDDriver != null)
                {
                    ddlIDDriver.Items.Clear();
                    ddlIDDriver.DataSource = mnTimeTable.getAllDriverActive();
                    ddlIDDriver.DataTextField = DriverColumn.FullName;
                    ddlIDDriver.DataValueField = "IDDriver";
                    ddlIDDriver.DataBind();
                    ddlIDDriver.SelectedValue = (item.FindControl("lblIDDriver") as Label).Text;
                }
                RadComboBox ddlIDBus = item.FindControl("ddlIDBus") as RadComboBox;
                if (ddlIDBus != null)
                {
                    ddlIDBus.Items.Clear();
                    ddlIDBus.DataSource = mnTimeTable.getAllBussActive();
                    ddlIDBus.DataTextField = BusColumn.RegistrationNumber;
                    ddlIDBus.DataValueField = BusColumn.IDBus;
                    ddlIDBus.DataBind();
                    ddlIDBus.SelectedValue = (item.FindControl("lblIDBus") as Label).Text;
                }
                List<ListItem> listItem = new List<ListItem>();
                listItem.Add(new ListItem("Chiều đi", "0"));
                listItem.Add(new ListItem("Chiều về", "1"));
                RadComboBox ddlDirection = item.FindControl("ddlDirection") as RadComboBox;
                if (ddlDirection != null)
                {
                    ddlDirection.DataSource = listItem;
                    ddlDirection.DataBind();
                }
            }
            else
            {
                Button btnUpdate = new Button();
                btnUpdate = item.FindControl("UpdateButton") as Button;
                btnUpdate.Text = "Chỉnh sửa";
                btnUpdate.CssClass = "myButton";
                RadComboBox ddlNameBusTrip = item.FindControl("ddlNameBusTrip") as RadComboBox;
                if (ddlNameBusTrip != null)
                {
                    ddlNameBusTrip.Items.Clear();
                    ddlNameBusTrip.DataSource = mnTimeTable.getAllBusTripNone();
                    ddlNameBusTrip.DataTextField = BusStripColumn.NameBusTrip;
                    ddlNameBusTrip.DataValueField = BusStripColumn.IDBusTrip;
                    ddlNameBusTrip.DataBind();
                    ddlNameBusTrip.SelectedValue = (item.FindControl("lblBusTripID") as Label).Text;
                }
                RadComboBox ddlIDDriver = item.FindControl("ddlIDDriver") as RadComboBox;
                if (ddlIDDriver != null)
                {
                    ddlIDDriver.Items.Clear();
                    ddlIDDriver.DataSource = mnTimeTable.getAllDriverNone();
                    ddlIDDriver.DataTextField = DriverColumn.FullName;
                    ddlIDDriver.DataValueField = "IDDriver";
                    ddlIDDriver.DataBind();
                    ddlIDDriver.SelectedValue = (item.FindControl("lblIDDriver") as Label).Text;
                }
                RadComboBox ddlIDBus = item.FindControl("ddlIDBus") as RadComboBox;
                if (ddlIDBus != null)
                {
                    ddlIDBus.Items.Clear();
                    ddlIDBus.DataSource = mnTimeTable.getAllBussNone();
                    ddlIDBus.DataTextField = BusColumn.RegistrationNumber;
                    ddlIDBus.DataValueField = BusColumn.IDBus;
                    ddlIDBus.DataBind();
                    ddlIDBus.SelectedValue = (item.FindControl("lblIDBus") as Label).Text;
                }
                List<ListItem> listItem = new List<ListItem>();
                listItem.Add(new ListItem("Chiều đi", "0"));
                listItem.Add(new ListItem("Chiều về", "1"));
                RadComboBox ddlDirection = item.FindControl("ddlDirection") as RadComboBox;
                if (ddlDirection != null)
                {
                    ddlDirection.DataSource = listItem;
                    ddlDirection.DataBind();
                    String direct = (item.FindControl("lblIDDrirection") as Label).Text;
                    if (direct.Equals("Chiều đi"))
                    {
                        ddlDirection.SelectedIndex= 0;
                    }
                    else
                    {
                        ddlDirection.SelectedIndex = 1;
                    }
                }
            }
            Button btnCancel = new Button();
            btnCancel = item.FindControl("CancelButton") as Button;
            btnCancel.Text = "Hủy bỏ";
            btnCancel.CssClass = "myButton";
            
        }
    }
}